<template>
  <base-breadcrumb :title="title">
    <!-- 职工代表大会编辑 -->
    <a-spin :spinning="loading">
      <a-form-model ref="ruleForm" :model="form" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }" :rules="rules">
        <!-- 表格 -->
        <a-card :bordered="false">
          <title-name title="经典方案"></title-name>
          <custom-form :isEmit="false" :formList="formList" :form="form" :editModel="editModel == 0 ? '' : ''" @selectOrg="selectOrg">
          </custom-form>
        </a-card>

        <a-card :bordered="false">
          <title-name title="推优"></title-name>
          <custom-form :isEmit="false" :formList="pushList" :form="form" :editModel="editModel == 0 ? '' : ''">
          </custom-form>
        </a-card>
      </a-form-model>

      <!-- 页脚操作栏 -->
      <div class="btn_box page-btn-right-top">
        <a-button
          class="button_right"
          v-if="form.auditStatus == 5"
          type="primary"
          :loading="loadingBtn.REJECTED"
          @click="handleSubmit('REJECTED')"
          >保存</a-button
        >
        <a-button class="button_right" type="primary" :loading="loadingBtn.SUBMITTED" @click="handleSubmit('SUBMITTED')"
          >提交</a-button
        >
        <a-button @click="handleActiveClose">关闭</a-button>
      </div>
    </a-spin>
  </base-breadcrumb>
</template>

<script>
import {
  getPlanHighlightsList,
  getPlanHighlights,
  addPlanHighlights,
  addPlanHighlightsPush
} from '@/api/technical/planHighlights'
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import SelectPersonModal from '@/components/SelectPersonModal'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 分公司
import SelectPersonByProject from '@/components/Select/SelectPersonByProject'
import { getList, getDetails } from '@/api/technical/approval'
import { planClassic } from './model'
export default {
  name: 'a' + Date.now(),
  extends: new BaseEditPage(),
  components: {
    SelectPersonModal,
    SelectPersonByProject
  },
  data() {
    return {
      loading: false, // 加载控件
      visible: false, //弹窗控件
      editModel: 0, // 操作状态:0 查看;1 编辑;2 新增
      tjj: false, //是否推局级
      form: {
        planTypeId: null,
        currentVersion: null,
        tableNumber: null,
        documentCode: null,
        preparedPeople: null,
        preparedDate: null,
        planInfo: null,
        textPageNum: null,
        appendixNum: null,
        professionalSubcontractProgram: null,
        fileList: null,
        pushOfficeId: null,
        pushOfficeInfo: null,
        pushOfficeFileList: null
      }, // 表单
      id: '',
      loadingBtn: {
        DELETE: false,
        DRAFT: false,
        SUBMITTED: false,
        REJECTED: false
      },
      rules: {
        pushTypeId: [{ required: true, message: '请选择' }],
        planId: [{ required: true, message: '请选择' }],
        projectId: [{ required: true, message: '请选择' }],
        branchCompanyId: [{ required: true, message: '请选择' }],
        pushInfo: [{ required: true, message: '请输入' }],
        pushOfficeInfo: [{ required: true, message: '请输入' }]
      },
      loadData: () => {
        return new Promise((resolve, reject) => {
          resolve({
            data: this.form.technicalReviewMonthPlans || []
          })
        })
      },
      planNameList: [],
      userInfo: JSON.parse(sessionStorage.getItem('ycloud-user_info')),
      init: false, // 初始化控件
      title: '经典方案推局级',
      // titles: ['经典方案查看', '经典方案编辑', '经典方案新增']
    }
  },
  props: {
    isAudit: {
      // 是否作为审批详情
      type: Boolean,
      default: false
    }
  },
  watch: {
    isAudit: {
      immediate: true,
      handler(val) {
        if (val) {
          this.$nextTick(() => {
            this.editModel = 0
          })
        }
      }
    }
  },
  mounted() {
    // 不为添加时请求详情

    if (this.editModel != 2) {
      this.getDetails()
    } else {
      this.$nextTick(() => {
        this.init = true
      })
    }
  },
  computed: {
    formList() {
      return [
        {
          label: '所属单位',
          type: 'orgTree',
          keyName: 'projectId',
          rules: [{ required: true, message: '请选择项目名称', trigger: 'change' }],
          span: 16,
          isAutoSelectFirst: true,
          isNeedAuth:true,
          checkStrictly: true,
          statusSet:[1,2,3,4,5,6],
          disabled:this.editModel == 0 || this.userInfo?.currentPost?.code != 'CO',
          isQueryProjecDetail: false
        },
        // {
        //   type: SelectSubCompany,
        //   keyName: 'branchCompanyId',
        //   label: '分公司名称',
        //   //分公司人添加数据不可选择分公司
        //   disabled: this.editModel == 0 || this.userInfo?.currentPost?.code != 'CO',
        //   attrs: {
        //     autoSelected: this.editModel == 2,
        //     getAll:this.editModel == 0,
        //   }
        // },
        // {
        //   type: SelectProject,
        //   keyName: 'projectId',
        //   label: '项目名称',
        //   disabled: this.editModel == 0,
        //   attrs: {
        //     autoSelected: this.editModel == 2,
        //     keyValue: 'branchCompanyId',
        //     isClear: this.editModel == 2,
        //     isEdit: this.editModel != 2,
        //     isSelf: this.editModel != 0,
        //     form: this.form
        //   },
        //   selectChange: async e => {
        //     this.loading = true
        //     let approval = await getList({
        //       branchCompany: this.form.branchCompanyId,
        //       projectName: e.code,
        //       pageNo: 1,
        //       pageSize: 10000
        //     })
        //
        //     let approvalList = approval.data.map(i => {
        //       return {
        //         label: i.schemeName,
        //         value: i.id
        //       }
        //     })
        //
        //     let highlights = await getPlanHighlightsList({
        //       needCount: false,
        //       query: {
        //         projectId: e.code,
        //         branchComId: this.form.branchCompanyId
        //       }
        //     })
        //
        //     let highlightslList = highlights.data.map(i => {
        //       return {
        //         label: i.planName,
        //         value: i.planId
        //       }
        //     })
        //     this.loading = false
        //     console.log(approvalList)
        //     console.log(highlightslList)
        //
        //     //根据id去重
        //     highlightslList.forEach(i => {
        //       approvalList.forEach((e, index) => {
        //         if (i.value == e.value) {
        //           approvalList.splice(index, 1)
        //         }
        //       })
        //     })
        //     //非新增把当前的数据添加
        //     if (this.editModel != 2) {
        //       approvalList.unshift({ label: this.form.planName, value: this.form.planId })
        //     }
        //     this.planNameList = approvalList
        //     console.log(this.planNameList)
        //   }
        // },
        {
          keyName: 'planId',
          disabled: this.editModel == 0,
          type: 'a-select',
          label: '方案名称',
          attrs: {
            showSearch: true,
            optionLabelProp: 'children'
          },
          data: this.planNameList,
          change: e => {
            this.loading = true
            //数据赋值到经典方案
            getDetails({ id: e }).then(res => {
              this.loading = false
              console.log(res)
              this.form.planName = res.data.schemeName
              this.form.planTypeId = res.data.programTypeName
              this.form.currentVersion = res.data.currentVersion
              this.form.tableNumber = res.data.reviewCode
              this.form.documentCode = res.data.fileCode
              this.form.preparedPeople = res.data.compiler
              this.form.preparedDate = res.data.reportingTime
              this.form.planInfo = res.data.programOverview
              this.form.textPageNum = res.data.bodyPages
              this.form.appendixNum = res.data.attachmentPages
              this.form.professionalSubcontractProgram =
                res.data.whetherProfessionalSubcontracting === 'true' ? true : false
              this.form.fileList = res.data.fileInfoVOList
            })
          }
        },
        {
          keyName: 'planTypeId',
          type: 'BaseInput',
          label: '方案类型',
          disabled: true
        },
        {
          keyName: 'currentVersion',
          type: 'BaseInput',
          label: '现行版本',
          disabled: true
        },
        {
          keyName: 'tableNumber',
          type: 'BaseInput',
          label: '报审表编号',
          disabled: true
        },
        {
          keyName: 'documentCode',
          type: 'BaseInput',
          label: '文件编码',
          disabled: true
        },

        {
          keyName: 'preparedPeople',
          type: SelectPersonByProject,
          disabled: true,
          label: '编制人员',
          attrs: {
            projectId: this.form.projectId,
            showSearch: true,
            orgId: this.form.projectDeptId,
            mode: 'combobox',
            optionLabelProp: 'children'
          }
        },

        {
          keyName: 'preparedDate',
          type: 'BaseInput',
          label: '编制日期',
          disabled: true
        },
        {
          keyName: 'planInfo',
          type: 'BaseTextarea',
          label: '方案概况',
          disabled: true,
          span: 24
        },
        {
          keyName: 'textPageNum',
          type: 'BaseInput',
          label: '正文页数',
          disabled: true
        },
        {
          keyName: 'appendixNum',
          type: 'BaseInput',
          label: '附件页数',
          disabled: true
        },
        {
          keyName: 'professionalSubcontractProgram',
          type: 'a-switch',
          label: '是否为专业分包方案',
          disabled: true
        },
        {
          keyName: 'fileList',
          type: 'uploadFile',
          disabled: true,
          label: '附件上传',
          span: 24,
          businessCode: 'BIM03'
        }
      ]
    },
    pushList() {
      return [
        {
          keyName: 'pushOfficeId',
          type: 'a-select',
          label: '经典方案',
          disabled: true,
          data: planClassic
        },
        {
          keyName: 'pushOfficeInfo',
          type: 'BaseTextarea',
          label: '推优理由',
          span: 24
        },
        {
          keyName: 'pushOfficeFileList',
          type: 'uploadFile',
          label: '附件上传',
          hiddenRemark: true,
          span: 24,
          businessCode: 'BIM03',
          remark: '备注：上传相关证明和支撑文件'
        }
      ]
    }
  },
  methods: {
    async selectOrg(val) {
      this.form.branchCompanyId = val[val.length - 2].orgId
      this.loading = true
      let approval = await getList({
        branchCompany: this.form.branchCompanyId,
        projectName: this.form.projectId,
        pageNo: 1,
        pageSize: 10000
      })

      let approvalList = approval.data.map(i => {
        return {
          label: i.schemeName,
          value: i.id
        }
      })
      let highlights = await getPlanHighlightsList({
        needCount: false,
        query: {
          projectId: this.form.projectId,
          branchComId: this.form.branchCompanyId
        }
      })

      let highlightslList = highlights.data.map(i => {
        return {
          label: i.planName,
          value: i.planId
        }
      })
      this.loading = false
      console.log(approvalList)
      console.log(highlightslList)

      //根据id去重
      highlightslList.forEach(i => {
        approvalList.forEach((e, index) => {
          if (i.value == e.value) {
            approvalList.splice(index, 1)
          }
        })
      })
      //非新增把当前的数据添加
      if (this.editModel != 2) {
        approvalList.unshift({ label: this.form.planName, value: this.form.planId })
      }
      this.planNameList = approvalList
      console.log(this.planNameList)
    },
    getDetails() {
      this.loading = true
      getPlanHighlights({ id: this.id })
        .then(res => {
          this.loading = false
          this.form = { ...this.form, ...res.data }
          if (this.tjj) {
            this.form.pushOfficeId = '2'
          }
          this.$nextTick(() => {
            this.init = true
          })
        })
        .catch(() => {
          this.loading = false
        })
    },
    /**
     * 提交表单
     */
    validateForm() {
      let flag = false
      this.$refs.ruleForm.validate(e => {
        if (e) {
          flag = true
        }
      })
      return flag
    },
    /**
     * 提交表单
     */
    handleSubmit(status) {
      if (status == 'SUBMITTED' && !this.validateForm()) {
        return
      }
      this.loading = true
      this.loadingBtn[status] = true

      let form = this.$clone(this.form)
      form.auditStatus = status
      form.peopleCode = this.$userInfo().currentPost.postCode
      addPlanHighlights(form)
        .then(() => {
          this.loading = false
          this.loadingBtn[status] = false
          this.$message.success(status == 'SUBMITTED' ? '提交成功！' : '保存成功')
          this.$multiTab.close()
        })
        .catch(() => {
          this.loadingBtn[status] = false
          this.loading = false
          this.$message.warning(err.data.message)
        })
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .form-item  {
  .ant-input:not([disabled]),
  .ant-select:not(.ant-select-disabled) .ant-select-selection,
  .ant-calendar-picker-input,
  .ant-input-number:not([disabled]),
  .ant-cascader-picker {
    border-color: #e4ac0b;
    background-color: #fff9e8;
  }
  .ant-input-disabled,
  .ant-input-number-disabled .ant-input-number-input,
  .ant-time-picker-input[disabled] {
    border-color: #d9d9d9 !important;
    background-color: #f5f5f5 !important;
  }
  .remark {
    .ant-input {
      border-color: inherit;
      background-color: initial;
    }
  }
}
</style>
